//
//  ComposeViewController.swift
//  CJPWB
//
//  Created by 陈锦彭 on 16/3/21.
//  Copyright © 2016年 chenjinpeng. All rights reserved.
//

import UIKit
import SVProgressHUD
class ComposeViewController: UIViewController {
    // MARK: - 属性
    ///中间标题
   private lazy var titleView : ComposeTitleView = ComposeTitleView()
    ///textView
    @IBOutlet weak var customTextView: ComposeTextView!
    ///工具条
    @IBOutlet weak var toolBar: UIToolbar!
    ///图片选择界面
    private lazy var picturePickerVC : PicturePickerViewController = PicturePickerViewController(collectionViewLayout: PicturePickerViewLayout())
    ///底部工具条的图片按钮
    @IBOutlet weak var pictureBtn: UIButton!
    
    
    // MARK: - 约束属性
    ///toolbar底部约束
    @IBOutlet weak var toolBarBottomCons: NSLayoutConstraint!
    
     // MARK: - 系统回调函数
    override func viewDidLoad() {
        super.viewDidLoad()
      //设置导航条的内容
       setupNavigationBar()
        
       //添加图片选择控制器的View
        addPicturePickerView()
        
        
      //监听键盘弹出
        NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardWillChangeFrame:", name: UIKeyboardWillChangeFrameNotification, object: nil)
        
        customTextView.becomeFirstResponder()
    }
    
    //移除监听
    deinit {
    NSNotificationCenter.defaultCenter().removeObserver(self)
    }

}


//设置后UI相关
extension ComposeViewController {
    //设置导航条的内容
    private func setupNavigationBar() {
        //1.设置左边的按钮
        navigationItem.leftBarButtonItem = UIBarButtonItem(title: "取消", style: .Plain, target: self, action: "leftBarButtonItemClick")
        //2.设置右边的按钮
        navigationItem.rightBarButtonItem = UIBarButtonItem(title: "发送", style: .Plain, target: self, action: "rightBarButtonItemClick")
        //3.设置中间的标题
        titleView.frame = CGRect(x: 0, y: 0, width: 100, height: 36)
        navigationItem.titleView = titleView
        
    }
    
    
    ///添加图片选择器的View
    private func addPicturePickerView() {
        //1.添加图片选择器的View
        addChildViewController(picturePickerVC)
        self.view.insertSubview(picturePickerVC.view, belowSubview: toolBar)
        //2.添加约束
        picturePickerVC.view.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(self.view.snp_left)
            make.right.equalTo(self.view.snp_right)
            make.bottom.equalTo(self.view.snp_bottom)
            make.height.equalTo(0)
        }
        
    }
}



// MARK: - 点击事件的处理
extension ComposeViewController{
    //左边的死安吉事件
    @objc private func leftBarButtonItemClick() {
        dismissViewControllerAnimated(true, completion: nil)
    }
    
    //右边的发送微博按钮
    @objc private func rightBarButtonItemClick() {
        let textString = customTextView.text
        let image = picturePickerVC.pictureArray.first
        HttpRequestTool.shareInstance.sendStatus(textString, image: image) { (isSuccessed) -> () in
            if isSuccessed {
                SVProgressHUD.showSuccessWithStatus("发送成功")
            }else{
                SVProgressHUD.showErrorWithStatus("发送失败")
            }
        }
    }
    
    //件盘的frame改变时的监听
    @objc private func keyboardWillChangeFrame(note : NSNotification) {
        //1.取出键盘最终的frame
        let endFrameValue = note.userInfo![UIKeyboardFrameEndUserInfoKey] as! NSValue
        let  endFrameRect = endFrameValue.CGRectValue()
        //2.取出键盘变换过程中的时间
        let duration = note.userInfo![UIKeyboardAnimationDurationUserInfoKey] as! NSTimeInterval
        let keyBoardY = endFrameRect.origin.y
        toolBarBottomCons.constant = screenH - keyBoardY
        UIView.animateWithDuration(duration) { () -> Void in
            self.view.layoutIfNeeded()
        }
        
    }
    ///底部工具条图片选择按钮的点击事件
    @IBAction func toolbarPicPickerCilck(sender: AnyObject) {
        //1.退出键盘
        customTextView.resignFirstResponder()
        //2.添加约束
        picturePickerVC.view.snp_remakeConstraints { (make) -> Void in
            make.left.equalTo(self.view.snp_left)
            make.right.equalTo(self.view.snp_right)
            make.bottom.equalTo(self.view.snp_bottom)
            make.height.equalTo(self.view.snp_height).multipliedBy(0.65)
        }
        
        //3.更新布局
        UIView.animateWithDuration(0.25) { () -> Void in
            self.view.layoutIfNeeded()
        }
    }
   

}

// MARK: - textView的理方法
extension ComposeViewController : UITextViewDelegate {
    ///当内容发生改变时调用
    func textViewDidChange(textView: UITextView) {
        customTextView.placeholderLabel.hidden = textView.hasText()
        navigationItem.rightBarButtonItem?.enabled = textView.hasText()
    }
    
    //用户拖动时让键盘退出
    func scrollViewWillBeginDragging(scrollView: UIScrollView) {
        customTextView.resignFirstResponder()
    }
    
}
